/*
  Web Server
 
 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 */

#include <SPI.h>
#include <Ethernet.h>
#include "CC1101.h"
#include "Address.h"
#include "Temperature.h"
#include "InfraredRemote.h"
#include <OneWire.h>

CC1101 theCC1101;
byte RX_buffer[61]={0};
byte aRXsize;
byte TX_buffer[61]={10};
bool isSending;
int  i, ii;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

// Attribute to control the address
Address theAddress;

// Attribute to contorl the DS-Temperature sensor
Temperature theTemperature;
byte theDSAddress[8];

// Attribute to control the IR remote
InfraredRemote theIR;

void setup() 
{
    
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.println("WebDomotico");
  
  Serial.print("Address : ");
  Serial.println(theAddress.ReadAddress());

  if (theAddress.ReadAddress() == 0)
  {
    // Bus controller
    Serial.println("Master");
  }
  else
  {
    // Slave
    Serial.println("Slave");
    
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  
  theCC1101.Init();
  Serial.println("Setup CC1101 OK");
  isSending = true;
  
  theDSAddress[0]=0;
  theDSAddress[1]=0;
  theDSAddress[2]=0;
  theDSAddress[3]=0;
  theDSAddress[4]=0;
  theDSAddress[5]=0;
  theDSAddress[6]=0;
  theDSAddress[7]=0;
  theTemperature.Configure(23, theDSAddress); //TBD
  Serial.println("DS configured");
  
  theIR.Configure(24);//TBD
  Serial.println("IR Remote configured");
  
}


void loop() 
{
  float aDegree;
  
//Serial.println("Loop");
  if (isSending)
    {
        delay (1000);
    
        theCC1101.SendData(TX_buffer,61);

        theCC1101.SetReceive();
        
        isSending = false;
    }
    else
    {
        if(theCC1101.CheckReceiveFlag())
        {
            isSending = true;
          
            //digitalWrite(13, HIGH);
    
            aRXsize=theCC1101.ReceiveData(RX_buffer);
    
            for(i=0;i<aRXsize;i++)
            {
                Serial.print(RX_buffer[i],DEC);

            }
    
            Serial.println("");
    
            //digitalWrite(13, LOW);

        }

    }

// Read the temperature
aDegree = theTemperature.getState();

//  Serial.print("  Temperature = ");
  Serial.print(aDegree);
  Serial.println(" Celsius, ");

    
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
                    // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");       
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    //delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
  
  byte aData[8];
  aData[0] = 0;
  aData[1] = 0;
  aData[2] = 0;
  aData[3] = 0;
  aData[4] = 0;
  aData[5] = 0;
  aData[6] = 0;
  aData[7] = 0;
  
  theIR.SendData(aData, 8);
}

